<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Set  extends Base
{
   public function initialize()
    {
       parent::initialize();
       $this->isLogin();
    }
    public function index()
    {


      return $this->fetch();
    }

    public function info()
    {

      $param = $this->request->param();
      
       // 验证
        $rule =[    
        'nickname'=>'require',
        'qq'=>'require|number',
        'email' =>'require|email',
        ];
        $msg= [
         'nickname.require' =>'昵称不能为空',   
         'qq.require' =>'请输入QQ号码',
         'qq.number' =>'请输入正确的QQ号码',
         'email.require' =>'请输入邮箱',
         'email.email' =>'请输入正确的邮箱',

        ];
         $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
       if($error){
              return error_json($error);
         }

        $data['qq']       = $param['qq'];
       
        if($param['mobile'] != $this->user->mobile){
          if(!empty($param['mobile'])){
             $data['mobile']       = $param['mobile'];
             $data['is_mobile']       = 0;
          }
        }

    		$data['nickname'] = addslashes($param['nickname']);
    	     // 检查昵称是否存在
           $user_where=[];
           $user_where[] = ['nickname','=',$data['nickname']];
           $user_where[] = ['uid','<>',session('uid')];
           $nickname = model('user')->infoData($user_where);
           if(!empty($nickname)){
             return error_json('昵称已经存在');
           }
        if($this->user['email'] != $param['email']){
          $data['is_email']  = 0;
          $data['email']  = $param['email'];
        }

    		$data['gender'] = intval($param['gender']);

    		$data['provid'] = intval($param['prov']);
    		$data['cityid'] = intval($param['city']);
    		$data['distid'] = intval($param['dist']);
        $where['uid']   = session('uid');



	      model('user')->editData($data,$where);

          session('__token__',null);  //清空session token
	      return success_json('修改成功');

    }

   //修改登陆密码
   public function editPass()
   {
     if($this->request->isAjax()){
         $param = $this->request->param();    

        $rule =[    
          'nowpass'=>'require',
          'pass'=>'require|min:6|max:16',
          'repass'=>'require',
        ];
        $msg= [
         'nowpass.require' =>'请输入当前密码',
         'pass.require' =>'请输入新密码',
         'pass.min' =>'请输入正确的新密码',
         'pass.max' =>'请输入正确的新密码',
         'repass.require' =>'请输入确认密码',   
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
       if($error){
              return error_json($error);
         }
       
         if($this->user['password'] != password_key(session('username'),$param['nowpass'],$this->user['passsalt']))
         {
             return error_json('当前密码错误');
         }
         if($param['pass'] !=  $param['repass']){
             return error_json('确认密码错误');
         }
  
         $data['password']    = password_key(session('username'),$param['pass'],$this->user['passsalt']); 
         $where['uid']   = session('uid');
         model('user')->editData($data,$where);

  
         return success_json('密码修改成功');
     }

   }
   //修改登陆密码
   public function editPayPass()
   {
     if($this->request->isAjax()){
         $param = $this->request->param();    
         $rule =[    
          'nowpayword' => 'require',
          'payword'    => 'require|min:6|max:16',
          'repayword'  => 'require',
        ];
        $msg= [
         'nowpayword.require' => '请输入当前支付密码',
         'payword.require'    => '请输入新支付密码',
         'payword.min'        => '请输入正确的新支付密码',
         'payword.max'        => '请输入正确的新支付密码',
         'repayword.require'  => '请输入确认支付密码',   
        ];
         $error = $this->checkSubmit($param,$rule,$msg);  //调用common 公共控制器中的 checkSubmit 方法
       if($error){
              return error_json($error);
         }

         if($this->user['payword'] != payword_key(session('username'),$param['nowpayword'],$this->user['paysalt']))
         {
             return error_json('当前支付密码错误');
         }
         if($param['payword'] !=  $param['repayword']){
             return error_json('确认支付密码错误');
         }
  
         $data['payword']    = payword_key(session('username'),$param['payword'],$this->user['paysalt']); 
         $where['uid']   = session('uid');
         model('user')->editData($data,$where);
         return success_json('支付密码修改成功');
     }

   }
   //上传头像
   public function avatar()
   {
     if($this->request->isAjax()){
         $param = $this->request->param();    
         $data['avatar'] = $param['avatar'];
         $where['uid']   = session('uid');
         model('user')->editData($data,$where);
         return success_json('头像更新成功');
     }

   }


}
